<?php
/**
 * Description of Informationclass
 *
 * @author Chisheng Lu, Jeffy_shih
 * @copyright DoRISE team, Science Education center, NTNU.
 * @since 2010-05-03
 * @version 1.1
 * @copyright Copyright(c)2010 DoRISE Team
 */
class Information {
    //put your code here
    private $mysqli;

    function __construct() {
        //引入資料庫連結參數設定
        include '../inc/oaConn.inc.php';
        $this->mysqli = @new mysqli($mysqlHost,$mysqlUser,$mysqlPassword,$mysqlDB);
        if(mysqli_connect_errno()) {
            throw new Exception('無法建立資料庫連線！'.mysqli_connect_error());
            $this->mysqli = false;
            exit();
        }

        //連線使用編碼
        $this->mysqli->query("SET NAMES 'utf8'");
    }

    /**
     *
     * @param String $strInfoContent
     * @return Integer
     */
    public function addInformation($strInfoContent) {
        $intRtn = 0;
        $strNow = $this->getNowTime();
        $sql_addIn = 'INSERT INTO `Information` (`InfoContent`)';
        $sql_addIn .= ' VALUES(\'' . $strInfoContent . '\')';

        $rs_addIn = $this->mysqli->query($sql_addIn);
        if (!$rs_addIn) {
            throw new Exception('新增說明資料時發生錯誤！');
        }else{
            $intRtn = $this->mysqli->insert_id;
        } 
        
        return $intRtn;
    }

    /**
     *
     * @param Integer $intInfoId
     * @param String $strInfoContent
     * @return Boolen true:Update success
     */
    public function editInformation($intInfoId, $strInfoContent) {
        //檢查說明資料編號是否為空白
        if (empty ($intInfoId)) {
            throw new Exception('缺少說明資料編號！');
        }
        $bolRtn = false;
        
        $sql_editIn = 'UPDATE `Information` SET ';
        $sql_editIn .= '`InfoContent` = \'' . $strInfoContent . '\' ';
        $sql_editIn .= 'WHERE `InfoId` = ' . $intInfoId;
        
        $rs_editIn = $this->mysqli->query($sql_editIn);
        if (!$rs_editIn) {
            throw new Exception('更新試卷說明資料時發生錯誤！');
        }else{
            $bolRtn = true;
        }
        return $bolRtn;
    }

    /**
     *
     * @param Integer $intInfoId
     * @return Boolean
     */
    public function delInformation($intInfoId) {
        $bolRtn = false;
        //檢查說明資料編號是否為空白
        if (empty ($intInfoId)) {
            throw new Exception('錯誤！缺少說明資料編號。');
        }

        //刪除
        $sql_delIn = 'DELETE FROM `Information` WHERE `InfoId`=' . $intInfoId;
        $rs_delIn = $this->mysqli->query($sql_delIn);
        if (!$rs_delIn) {
            throw new Exception('錯誤！刪除說明資料時發生錯誤。');
        }else{
            $bolRtn = true;
        }
        return $bolRtn;
    }

    /**
     *
     * @param Integer $intInfoId Exam-Information Id
     * @return Array One dimension array
     */
    public function getInformationInfoByInfoId($intInfoId) {
        //檢查說明資料編號是否為空白
        if (empty ($intInfoId)) {
            throw new Exception('缺少說明資料編號！');
        }

        $sql_getInfoById = 'SELECT * FROM `Information` WHERE `InfoId`=' . $intInfoId;
        $rs_getInfoById = $this->mysqli->query($sql_getInfoById);

        if (!$rs_getInfoById) {
            throw new Exception('無法查詢該說明資料的資料！');
        }else{
            if ($rs_getInfoById->num_rows == 0) {
                throw new Exception('無此說明資料編號的資料！');
            }else{
                $row_getInfoById = $rs_getInfoById->fetch_row();
                return $row_getInfoById;
            }
        } //End If

    }

    /**
     *
     * @param Integer $intInfoId
     * @return Integer: true: Locked, false: unLocked
     */
    public function chkExamLockByInfoId($intInfoId) {
        //檢查試卷說明編號是否為空白
        if (empty ($intEBId)) {
            throw new Exception('缺少該試卷的說明編號！');
        }

        include 'ExamProcess.Class.php';

        $sql_getEBById = 'SELECT `ExamBasic`.`EBLock` FROM `ExamBasic`
            INNER JOIN `ExamProcess` ON `ExamProcess`.`EBId`= `ExamBasic`.`EBId`
            WHERE `ExamProcess`.`EPQEId`=' . $intInfoId . ' AND `ExamProcess`.`EPCategory` = ' . ExamProcess::EPInformation;
        $rs_getEBById = $this->mysqli->query($sql_getEBById);

        if (!$rs_getEBById) {
            throw new Exception('無法查詢該說明的試卷基本資料的資料！');
        }else{
            if ($rs_getEBById->num_rows == 0) {
                throw new Exception('無此說明的試卷基本資料的資料！');
            }else{
                $row_getEBById = $rs_getEBById->fetch_row();
                return ($row_getEBById[0] == 1);
            }
        } //End If
    }

    /**
     *
     * @return Array
     */
    public function getInformationList() {
        $sql_getInList = 'SELECT * FROM `Information` ;';
        $rs_getInList = $this->mysqli->query($sql_getInList);

        if (!$rs_getInList) {
            throw new Exception('無法查詢該說明資料的資料！');
        }else{
            if ($rs_getInList->num_rows == 0) {
                throw new Exception('無此說明資料編號的資料！');
            }else{
                $aryInList = array();
                while ($aryOneIn = $rs_getInList->fetch_row()) {
                    array_push($aryInList, $aryOneIn);
                }
                return $aryInList;
            }
        } //End If
    }

    /**
     *
     * @return String
     */
    private function getNowTime() {
        return date('Y-m-d H:i:s');
    }

    function  __destruct() {
        if ($this->mysqli) {
            $this->mysqli->close();
            $this->mysqli = FALSE;
        }
    }
}
?>
